﻿using Com.JunCaiSoft.Common.Extension;
using Com.JunCaiSoft.Common.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Com.JunCaiSoft.Common.Controller
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class NASGeneralAPIController : ControllerBase
    {
        /// <summary>
        /// 配置
        /// </summary>
        private IConfiguration Configuration { get; set; }
        /// <summary>
        /// 配置环境
        /// </summary>
        private readonly IWebHostEnvironment HostingEnvironment;
        public NASGeneralAPIController(IWebHostEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                               .SetBasePath(env.ContentRootPath)
                               .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                               .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
                               .AddJsonFile("DataBaseExcute.json", optional: true, reloadOnChange: true);
            HostingEnvironment = env;
            Configuration = builder.Build();
        }
        /// <summary>
        /// 群辉NAS登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<ActionResult> LoginNAS()
        {
            var baseurl = Configuration.GetSection("NASCore:BaseURL");

            var username = Configuration.GetSection("NASCore:Username");

            var password = Configuration.GetSection("NASCore:Password");

            var NASLoginURL = baseurl.Value + "/auth.cgi?api=SYNO.API.Auth&version=3&method=login&account=" + username.Value + "&passwd=" + password.Value + "&session=FileStation&format=cookie";

            var result = JsonConvert.DeserializeObject<NASResult<LoginResult, Error>>(HttpExtension.HttpGet(NASLoginURL, true, "application/json;")) ;

            return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
        }
        /// <summary>
        /// 群辉NAS登出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<ActionResult> LogoutNAS()
        {
            var baseurl = Configuration.GetSection("NASCore:BaseURL");

            var username = Configuration.GetSection("NASCore:Username");

            var password = Configuration.GetSection("NASCore:Password");

            var NASLoginURL = baseurl.Value + "/auth.cgi?api=SYNO.API.Auth&version=3&method=logout&account=" + username.Value + "&passwd=" + password.Value + "&session=FileStation&format=cookie";

            var result = JsonConvert.DeserializeObject<NASResult<LoginResult, Error>>(HttpExtension.HttpGet(NASLoginURL, true, "application/json;"));

            return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
        }
    }
}
